package com.eascs.admin.o2o.badloan.dao;

import java.util.GregorianCalendar;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.badloan.entity.O2oCacleEntity;
import com.eascs.admin.o2o.badloan.entity.O2oCacleForm;
import com.eascs.admin.o2o.badloan.form.O2oCacleTForm;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;


/**
 * 
 * @author CXW
 *
 */

@Repository
public class O2oCancleDao extends BaseO2oDao{
	
	public O2oCacleEntity findByLoanapplyId(String loanapplyId) {
		String sql = "select * from o2o_loan_t_info where t_is_success='Y' and loanapply_no=?";
		return this.findEntity(sql, O2oCacleEntity.class, loanapplyId);
	}
	public O2oCacleTForm findByLoanapplyTId(String loanapplyId) {
		StringBuffer sql=new StringBuffer();
		sql.append("select f.loanapply_no as loanapplyNo,f.t_operno as tOperno,f.t_opername as tOpername,");
		sql.append(" f.t_time as tTime,f.t_type as tType, f.t_remark as tRemark,f.t_why as tWhy,");
		sql.append(" f.t_is_success as tIsSuccess,f.t_veto_term as tVetoTerm,f.t_sys_type as tSysType,e.bill_code as tbillcode,e.id as tid ");
		sql.append(" from o2o_loan_t_info f LEFT JOIN o2o_cacle e on f.loanapply_no=e.loanapply_no where f.t_is_success='Y' and f.loanapply_no=?");
		//		String sql = "select f.*,e.bill_code as tbillcode,e.id as tid from o2o_loan_t_info f LEFT JOIN o2o_cacle e on f.loanapply_no=e.loanapply_no where f.t_is_success='Y' and f.loanapply_no=?";
		return this.findEntity(sql.toString(), O2oCacleTForm.class, loanapplyId);
	}
	public PageResult<Object[]> findCancelLoanByPage(O2oCacleForm o2oCacleForm) {
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class, o2oCacleForm.getPageSize(), o2oCacleForm.getPageIndex());
		String querySql = "sql.o2o.cancle.querybypage";
		StringBuffer sql = new StringBuffer(this.getSql(querySql));
		if (StringUtil.hasText(o2oCacleForm.getBillCode())) {
			sql.append(" and T.bill_code like ?");
			pageQuery.addLikeParam(o2oCacleForm.getBillCode());
		}

		if (StringUtil.hasText(o2oCacleForm.getUserSurname())) {
			sql.append(" and user_full_name like ?");
			pageQuery.addLikeParam(o2oCacleForm.getUserSurname());
		}

		if (StringUtil.hasText(o2oCacleForm.getOperatorName())) {
			sql.append(" and operator_name like ?");
			pageQuery.addLikeParam(o2oCacleForm.getOperatorName());
		}

		if (StringUtil.hasText(o2oCacleForm.getLoadName())) {
			sql.append(" and ltdName like ?");
			pageQuery.addLikeParam(o2oCacleForm.getLoadName());
		}

		if (!StringUtil.isEmpty(o2oCacleForm.getHostProvince())) {
			sql.append(" AND  province_code  = ? ");
			pageQuery.addParam(o2oCacleForm.getHostProvince());
		}

		if (StringUtil.hasText(o2oCacleForm.getStartTime())) {
			sql.append(" and t_time >= ? ");
			pageQuery.addParam(o2oCacleForm.getStartTime());
		}
		if (StringUtil.hasText(o2oCacleForm.getEndTime())) {
			sql.append(" and t_time <= ? ");
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(DateUtils.formateStandDate(o2oCacleForm.getEndTime()));
			gc.add(5, 1);
			pageQuery.addParam(gc.getTime());
		}
		if (StringUtil.hasText(o2oCacleForm.getLoanType())) {
			sql.append(" and loanType in (");
			String loanTypeA [] = {};  
			String loanType = "";
			StringBuffer loanTypes = new StringBuffer();
			if(o2oCacleForm.getLoanType().length()>4){
				loanTypeA = o2oCacleForm.getLoanType().split(",");
				for (int i = 0; i < loanTypeA.length; i++) {
					loanTypes.append("'" + loanTypeA[i] + "',");
				}
			}else{
				loanTypes.append("'"+o2oCacleForm.getLoanType()+"',");
			}
			loanType = loanTypes.toString().substring(0,loanTypes.toString().length()-1);
			sql.append(loanType);
			sql.append(")");
		}

		sql.append(" ORDER BY t_time desc ");
		pageQuery.setQuerySql(sql.toString());
		return this.findDataWithPage(pageQuery);
	}
}
